//
//  ActionView.swift
//  ShareActionView
//
//  Created by 郑振兴 on 15/11/23.
//  Copyright © 2015年 北京海尔广科数字技术有限公司－郑振兴. All rights reserved.
//

import UIKit

class ActionView: UIView {
    
    var delegate :ActionViewDelegate?
    
    fileprivate var showRect:CGRect = {
       
        let rect = UIScreen.main.bounds;
        
        return rect;
    }()
    
    fileprivate var hideRect:CGRect = {
        
        let rect = CGRect(x: 0, y: UIScreen.main.bounds.size.height, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.height);
        return rect;
        
    }()
    
    let bgView: UIView = {
        let view  = UIView()
        view.backgroundColor = UIColor.lightGray;
        view.alpha = 0.7;
        return view;
    }()
    
    init(view:UIView, title:String, items:[String], target: ActionViewDelegate,itemHeight:Int){
        super.init(frame:UIScreen.main.bounds);
        self.frame = self.hideRect;
        self.backgroundColor = UIColor.clear;
        
        self.bgView.frame = self.bounds;
        self.addSubview(self.bgView);
        
        
        self.CreateItem(items,itemHeight: itemHeight);
        self.CreateCancleItemAndTitleItem(title, items: items, itemHeight: itemHeight);
        
        self.delegate = target;
        
        view.addSubview(self);
    }
    
    
    func CreateItem(_ items:[String],itemHeight:Int){
        
        for (index,title) in items.enumerated(){
            
            let item = UIButton(type: .custom);
            
            item.frame = CGRect(x: 20, y: self.bounds.size.height-CGFloat(itemHeight*items.count + itemHeight+20) + CGFloat(itemHeight*index), width: UIScreen.main.bounds.size.width-40, height: CGFloat(itemHeight));
            item.backgroundColor = UIColor.white;
            item.tag = index;
            item.setTitle(title, for: UIControlState());
            item.setTitleColor(UIColor.black, for: UIControlState());
            item.addTarget(self, action: #selector(ActionView.itemTouchEvent(_:)), for: .touchUpInside);
            let line = UIView(frame: CGRect(x: 0,y: CGFloat(itemHeight)-0.5,width: UIScreen.main.bounds.size.width-40,height: 0.5));
            line.backgroundColor = UIColor.gray;
            item.addSubview(line);
            self.addSubview(item);
        }
        
    }
    
    func CreateCancleItemAndTitleItem(_ title:String, items:[String],itemHeight:Int){
        
        let titleLab = UILabel(frame: CGRect(x: 20, y: self.bounds.size.height-CGFloat(itemHeight*items.count + itemHeight+20) - CGFloat(itemHeight+10)-0.5, width: UIScreen.main.bounds.size.width-40, height: CGFloat(itemHeight+10)));
        titleLab.textAlignment = .center;
        titleLab.backgroundColor = UIColor.white;
        titleLab.textColor = UIColor.black;
        titleLab.font = UIFont.boldSystemFont(ofSize: 20);
        titleLab.text = title;
        let line = UIView(frame: CGRect(x: 20,y: self.bounds.size.height-CGFloat(itemHeight*items.count + itemHeight+20)-0.5,width: UIScreen.main.bounds.size.width-40,height: 0.5));
        line.backgroundColor = UIColor.black;
        titleLab.addSubview(line);
        self.addSubview(titleLab);
        
        
        let cancleButton = UIButton(type: .custom);
        cancleButton.frame = CGRect(x: 20, y: self.bounds.size.height-CGFloat(itemHeight), width: self.bounds.size.width - 40, height: CGFloat(itemHeight));
        cancleButton.backgroundColor = UIColor.white;
        cancleButton.setTitleColor(UIColor.red, for: UIControlState());
        cancleButton.setTitle("取消", for:UIControlState());
        cancleButton.addTarget(self, action: #selector(ActionView.cancleEvent(_:)), for: .touchUpInside);
        self.addSubview(cancleButton);
        
    }
    func show(){
        
        UIView.animate(withDuration: 0.5, animations: { () -> Void in
            self.frame = self.showRect;
        }) ;
    }
    fileprivate func hide(){
        
        UIView.animate(withDuration: 0.5, animations: { () -> Void in
            self.frame = self.hideRect;
            }, completion: { (finished) -> Void in
                self.removeFromSuperview();
        }) ;
        
    }
    func cancleEvent(_ item:UIButton){
        
        self.hide();
        
    }
    
    func itemTouchEvent(_ item:UIButton){
        self.hide();
        self.delegate?.itemClicked(item.tag);
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


protocol ActionViewDelegate{
    
    func itemClicked(_ index:Int);
}
